<?php
namespace app\admin\controller;
use app\system\model\Menu;

use think\Controller;
use think\Db;

class Base extends Controller
{
    static $admin_info = [];
    static $menu = [];
    public function __construct($module = '')
    {
        parent::__construct();

        $admin_info = session('admin_info');
        if(empty($admin_info)){
            $this->error('尚未登录','index.php/admin/login/login');
        }else{
            //传入控制器
            self::$admin_info = $admin_info;
            self::loadModuleMenu($module);
            //当前登录信息 传入模板
            $this->assign('admin_info',$admin_info);
        }
    }
    protected function loadMenu(){
        $result['left_menu']=[];
        $Mmenu = new \app\admin\model\Menu();
        $c = request()->controller();
        $a = request()->action();
        $pid = $Mmenu->_getPid($c,$a);
        //是否是内置的超级管理员
        if(self::$admin_info['id'] == config('code.is_admin')){
            $result['left_menu'] = $Mmenu ->_list('left_menu');
            $result['left_menu'] = json_decode(json_encode($result['left_menu']),true);
        }
        else{
            //获取用户角色
            $role = Db::name('auth_group')->alias('ag')
                ->join('auth_group_access agc','ag.id=agc.group_id','LEFT')
                ->where(['agc.uid'=>self::$admin_info['id']])->find();
            if(empty($role)){
                $this->error('没有权限，请联系管理员');
            }
            $role_rules = json_decode($role['rules'],true);
            $temp = $Mmenu ->_list('left_menu');
            foreach ($temp as $v){
                foreach ($role_rules as $v1){
                    if($v['menu_id'] == $v1['id']){
                        $con_temp = array_merge($v->getData(),$v1);
                        $result['left_menu'][] = $con_temp;
                        if($pid == $v['menu_id']){
                            $result['cur_menu'] = $con_temp;
                        }
                    }
                }
            }
        }
        $result['sub_top_menu'] = $Mmenu ->_list('sub_top_menu',$pid);
//        dump($result['sub_top_menu']);
        $result['sub_top_menu'] = json_decode(json_encode($result['sub_top_menu']),true);
        $result['cur_pid'] = $pid;
//        dump($result);
        self::$menu = $result;
        $this->assign($result);
    }

    protected function loadModuleMenu($module = ''){
        $result['left_menu']=[];
        $Mmenu = new \app\admin\model\Menu();
        $c = request()->controller();
        $a = request()->action();
        $pid = $Mmenu->_getPid($c,$a,$module);
        //是否是内置的超级管理员
        if(self::$admin_info['id'] == config('code.is_admin')){
            $result['left_menu'] = $Mmenu ->_list($module.'left_menu');
            $result['left_menu'] = json_decode(json_encode($result['left_menu']),true);
        }else{
            //获取用户角色
            $role = Db::name('auth_group')->alias('ag')
                ->join('auth_group_access agc','ag.id=agc.group_id','LEFT')
                ->where(['agc.uid'=>self::$admin_info['id']])->find();
            if(empty($role)){
                $this->error('没有权限，请联系管理员');
            }
            $role_rules = json_decode($role['rules'],true);
            $temp = $Mmenu ->_list($module.'left_menu');
            foreach ($temp as $v){
                foreach ($role_rules as $v1){
                    if($v['menu_id'] == $v1['id']){
                        $con_temp = array_merge($v->getData(),$v1);
                        $result['left_menu'][] = $con_temp;
                        if($pid == $v['menu_id']){
                            $result['cur_menu'] = $con_temp;
                        }
                    }
                }
            }
        }
        $result['sub_top_menu'] = $Mmenu ->_list($module.'sub_top_menu',$pid);
//        dump($pid);
        $result['sub_top_menu'] = json_decode(json_encode($result['sub_top_menu']),true);
        $result['cur_pid'] = $pid;
//        dump($result);exit;
        self::$menu = $result;
        $this->assign($result);
    }

}
